package model;

import java.util.ArrayList;
import java.util.List;

import model.connectible.ISP;
import model.connectible.Peer;
import model.connections.Connection;

public class Network {
	
	private List<Peer> allPeerList;
	private List<ISP> allISPList;
	
	public Network() {
		allISPList = new ArrayList<ISP>();
		allPeerList = new ArrayList<>();
	}

	public List<Peer> getAllPeerList() {
		return allPeerList;
	}

	public void setAllPeerList(List<Peer> allPeerList) {
		this.allPeerList = allPeerList;
	}

	public List<ISP> getAllISPList() {
		return allISPList;
	}

	public void setAllISPList(List<ISP> allISPList) {
		this.allISPList = allISPList;
	}
	
	public void addISP(ISP isp) {
		allISPList.add(isp);
	}
	
	public void addPeer(Peer peer) {
		allPeerList.add(peer);
	}
	
	public String toString() {
		String s = "";
		for (Peer peer : allPeerList) {
			s = s+"\n"+peer.toString();
		}
		s = s+"\n\n\n";
		for (ISP isp: allISPList) {
			s = s+"\n"+isp.toString();
		}
		return s;
	}
	
//	public Network clone() {
//		Network network = new Network();
//		List<Peer> peerList = new ArrayList<Peer>();
//		for (Peer peer : this.getAllPeerList()) {
//			peerList.add(peer.clone());
//		}
//		network.setAllPeerList(peerList);
//		
//		List<ISP> ispList = new ArrayList<ISP>();
//		for (ISP isp : this.getAllISPList()) {
//			ispList.add(isp.clone());
//		}
//		
//		return network;
//	}
	public Network getClone() {
		Network network = new Network();
		for (Peer peer : allPeerList) {
			peer.getClone().setIspConnection(
					new Connection(peer.getClone(), 
							((ISP)peer.getIspConnection().getOther(peer)).getClone(),
					peer.getIspConnection().getStability(), 
					peer.getIspConnection().getBandwidth()));
			network.addPeer(peer.getClone());
		}
		
		for (ISP isp : allISPList) {
			for (Connection c : isp.getLocalConnections()) {
				isp.getClone().addLocalConnection(new Connection(isp.getClone(),
						((Peer)c.getOther(isp)).getClone(),
						c.getStability(),
						c.getBandwidth()));
			}
			for (Connection c : isp.getIspConnections()) {
				isp.getClone().addISPConnection(new Connection(isp.getClone(),
						((ISP)c.getOther(isp)).getClone(),
						c.getStability(),
						c.getBandwidth()));
			}
			network.addISP(isp.getClone());
		}
		return network;
	}
}
